home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Graphismes / Bitmap / NIH Image 1.59 / Macros / Input⁄Output Macros < prev    next >
Text File  |  1995-06-14  |  14KB  |  574 lines

  1. var
  2.   CharLoc:integer; {Global variable, initially zero}
  3.  
  4. macro 'Save using Time as Name╔';
  5. {Note: Colons are not allowed in file names.}
  6. var
  7.   year,month,day,hour,minute,second,DayOfWeek:integer;
  8. begin
  9.   GetTime(year,month,day,hour,minute,second,DayOfWeek);
  10.   SaveAs(year-1900:2,'-',month:2,'-',day:2,
  11.          '/',hour:2,'-'minute:2,'-',second:2);
  12. end;
  13.  
  14.  
  15. macro 'Open with selection╔ [O]';
  16. begin
  17.   if nPics>0 then KillRoi; {Save Selection}
  18.   Open('');                {Prompt for file name}
  19.   RestoreROI;              {Transfer selection to new window}
  20. end;
  21.  
  22.  
  23. macro 'Save All╔';
  24. {
  25. Saves all currently open images in a folder using '001', '002', etc.
  26. as the file names. The save file dialog box will be displayed once
  27. (and only once) so that you can specify the folder to save the files in.
  28. Leave the file name blank(e.g. SaveAs('')) to get a dialog box for each file.
  29. }
  30. var
  31.   n:integer;
  32. begin
  33.   RequiresVersion(1.45);
  34.   for n:=1 to nPics do begin
  35.     SelectPic(n);
  36.     SaveAs(n:3);
  37.     {Export(n:3);}
  38.   end;
  39. end;
  40.  
  41.  
  42. macro 'Import FITS╔';
  43. {
  44. Imports 8 and 16-bit FITS images. Refer to "FITS:A Flexible Image
  45. Transport System", Astronomy and Astrophysics Supplement
  46. Series 44, 1981, 363-370.
  47. }
  48. var
  49.   width,height,offset,HdrPid:integer;
  50.   recsize,maxrecs,maxlines,loc,i,line:integer;
  51.   BitPix,str:string;
  52. begin
  53.   RequiresVersion(1.52);
  54.   recsize:=2880;
  55.   maxrecs:=10; {Must be increased to handle headers longer than 360 lines.}
  56.   maxlines:=(recsize*maxrecs)/80;
  57.   width:=recsize; 
  58.   height:=maxrecs;
  59.   offset:=0;
  60.   SetImport('8-bit'); 
  61.   SetCustom(width,height,offset);
  62.   Import(''); {Read in header as an image, prompting for the file name.}
  63.   HdrPid:=PidNumber;
  64.   BitPix:=Concat(chr(GetPixel(108,0)),chr(GetPixel(109,0)));
  65.   if (BitPix<>' 8') and (BitPix<>'16') then begin
  66.     PutMessage('This macro only reads 8 and 16-bit FITS files');
  67.     SelectPic(HdrPid);
  68.     Dispose;
  69.     exit;
  70.   end;
  71.   str:=concat(chr(GetPixel(266,0)), chr(GetPixel(267,0)),
  72.     chr(GetPixel(268,0)), chr(GetPixel(269,0)));
  73.   width:=StringToNum(str);
  74.   str:=concat(chr(GetPixel(346,0)), chr(GetPixel(347,0)),
  75.     chr(GetPixel(348,0)), chr(GetPixel(349,0)));
  76.   height:=StringToNum(str);
  77.   loc:=0;
  78.   line:=0;
  79.   repeat
  80.     str:='';
  81.     for i:=1 to 3 do begin
  82.        str:=concat(str,chr(GetPixel(loc mod recsize, loc div recsize)));
  83.        loc:=loc+1;
  84.     end;
  85.     loc:=loc+77; {Skip to end of line}
  86.     line:=line+1;
  87.   until (line>=maxlines) or (str='END');
  88.   SelectPic(HdrPid);
  89.   Dispose;
  90.   offset:=recsize+recsize*((line*80) div recsize);
  91.   if BitPix=' 8'
  92.     then SetImport('8-bit')
  93.     else SetImport('16-bit Signed; Calibrate; Autoscale');
  94.   SetCustom(width,height,offset);
  95.   Import('');  {No prompt this time; Import remembers the name.}
  96.   if BitPix=' 8' then Invert;
  97.   FlipVertical;
  98. end;
  99.  
  100. macro 'Display FITS Header╔';
  101. var
  102.   i,j,loc:integer;
  103.   HdrPid,RecordSize,maxlines,line:integer;
  104.   MaxRecords:integer;
  105.   wname,str:string;
  106. begin
  107.   RequiresVersion(1.53);
  108.   RecordSize:=2880;
  109.   MaxRecords:=10;
  110.   maxlines:=(RecordSize*MaxRecords)/80;
  111.   SetImport('8-bit'); 
  112.   SetCustom(RecordSize,10,0);
  113.   Import(''); {Read in header as an image, prompting for the file name.}
  114.   HdrPid:=PidNumber;
  115.   SetFont('Monaco');
  116.   SetFontSize(9);
  117.   wname:=concat(WindowTitle,' Header');
  118.   NewTextWindow(wname,500,500);
  119.   loc:=0;
  120.   line:=1;
  121.   repeat
  122.     ChoosePic(HdrPid);
  123.     str:='';
  124.     for j:=1 to 80 do begin
  125.        str:=concat(str,chr(GetPixel(loc mod RecordSize, loc div RecordSize)));
  126.        loc:=loc+1;
  127.     end;
  128.     SelectWindow(wname);
  129.     writeln(str);
  130.     line:=line+1;
  131.   until (line>maxlines) or (pos('END',str)=1);
  132.   SelectPic(HdrPid);
  133.   Dispose;
  134.  end;
  135.  
  136.  
  137. macro 'Import Image TIFF File╔';
  138. {
  139. As an example of how to import a foreign file format, this macro reads
  140. the TIFF files created by Image. The format of an Image TIFF file
  141. is described in Appendix E of the Image manual.
  142. }  
  143. var
  144.   width,height,offset:integer;
  145. begin
  146.   width:=768; 
  147.   height:=1;
  148.   offset:=0;
  149.   SetImport('8-bit'); 
  150.   SetCustom(width,height,offset);
  151.   Import(''); {Read in header as an image, prompting for the file name.}
  152.   if not ((GetPixel(0,0)=77) and (GetPixel(0,0)=77)) then begin  {'MM'}
  153.     PutMessage('This is not a TIFF file.');
  154.     SelectPic(nPics); Dispose;
  155.     exit;
  156.   end;
  157.   width := (GetPixel(30,0)*256) + GetPixel(31,0);
  158.   height := (GetPixel(42,0)*256) + GetPixel(43,0);
  159.   SelectPic(nPics);  {The ID of the last window opened is equal to nPics.}
  160.   Dispose;
  161.   offset:=768;
  162.   SetCustom(width,height,offset);
  163.   Import('');  {No prompt this time; Import remembers the name.}
  164. end;
  165.  
  166.  
  167. macro 'Import Multiple Images per File╔';
  168. {
  169. Imports a series of 256x256 images contained in a single file, in this
  170. case an NIH Image stack with an arbitrary number of 256x256 slices.
  171. }
  172. var
  173.   offset,i,PicSize,HdrSize,width,height:integer;
  174. begin
  175.   HdrSize:= 768;
  176.   width:= 256;
  177.   height:=256;
  178.   PicSize:=width*height;
  179.   offset:=HdrSize;
  180.   SetImport('8-bit');
  181.   for I:=1 to 100 do begin  {Macro will terminate at eof}
  182.     SetCustom(width,height,offset);
  183.     Import('');
  184.     offset:=offset+PicSize;
  185.   end;
  186. end;
  187.  
  188.  
  189. macro 'Import 3D PET╔';
  190. var
  191.   HdrSize,width,height:integer;
  192. begin
  193.   HdrSize:= 0;
  194.   width:= 128;
  195.   height:=128;
  196.   SetImport('8-bit');
  197.   SetCustom(width, height, HdrSize, 100);
  198.   Import('');
  199. end;
  200.  
  201.  
  202. macro 'Convert Files╔';
  203. {
  204. Converts a set of raw data files(all in the same folder) with names
  205. in the form raw.001, raw.002, etc to TIFF or PICT.  As long as the
  206. converted files are saved in the same folder, you should
  207. only see two file dialog boxes(one for the first Import and one for
  208. the first SaveAs).
  209. }
  210. Var
  211.   i,nFiles:integer;
  212. begin
  213.   nFiles:=GetNumber('Number of files:',5);
  214.   for i:=1 to nFiles do begin
  215.     Import('raw.',i:3);
  216.     SetPicName('file',i:3);
  217.     SaveAs;
  218.     Dispose;
  219.   end;
  220. end;
  221.  
  222.  
  223. macro 'Import IPLab File';
  224. var
  225.    width,height,offset:integer;
  226. begin
  227.    width:=100; 
  228.    height:=1;
  229.    offset:=0;
  230.    SetImport('8-bit'); 
  231.    SetCustom(width,height,offset);
  232.    Import(''); {Read in header as an image, prompting for file name.}
  233.    width := (GetPixel(8,0)*256) + GetPixel(9,0);
  234.    height := (GetPixel(12,0)*256) + GetPixel(13,0);
  235.    Dispose;  
  236.    offset:=2120;  {The IPLab offset}
  237.    SetImport('16-bit Signed; Calibrate; Autoscale');
  238.    SetCustom(width,height,offset);
  239.    Import('');  {No prompt this time; Import remembers the name.}
  240. end;
  241.  
  242.  
  243. procedure ShowBioRadInfo;
  244. {Displays the contents of the 480(?) byte header at}
  245. {the end of Biorad MRC 600 Z Series files.}
  246. var
  247.   MaxInfoSize,offset:integer;
  248.   ch:string;
  249. begin
  250.   MaxInfoSize:=480;
  251.   SetCustom(MaxInfoSize,1,HdrSize+Width*Height);
  252.   SetImport('8-bit'); {Don't invert}
  253.   Import('');
  254.   GetRow(0,0,MaxInfoSize);
  255.   Dispose;
  256.   SetNewSize(460,100);
  257.   SetForeground(255);
  258.   SetBackground(0);
  259.   MakeNewWindow('Info');
  260.   SetCursor('Watch');
  261.   SetFont('Monaco');
  262.   SetText('With background; Left Justified');
  263.   SetFontSize(12);
  264.   MoveTo(8,10);
  265.   for i:=0 to MaxInfoSize-1 do begin
  266.     offset:=i mod 96;
  267.     if offset=0 then writeln;
  268.     ch:=chr(LineBuffer[i]);
  269.     if (offset=2) and (ord(ch)=0) then exit;
  270.     if (offset>=16) and (offset<=95) and (ord(ch)>=32) and (ord(ch)<=126)
  271.       then write(ch);
  272.   end;
  273. end;
  274.  
  275.  
  276. macro 'Import Biorad MRC 600 Z Series╔';
  277. {
  278. Imports a Z series(multiple images per file) from a Biorad MRC 600
  279. confocal microscope.  The width, height and number of images are
  280. extracted from the first 3 16-bit word in the 76 byte header and
  281. the file name is extracted from bytes 18-23 of the header. This macro
  282. does not read merged pseudocolored BioRad files. Note that the Undo
  283. and Clipboard buffers must be set to 384K to work with the typical
  284. 768x512 Biorad images.
  285. }
  286. var
  287.   width,height,nImages,offset,hdrsize,i,start,picsize:integer;
  288. begin
  289.   RequiresVersion(1.50);
  290.   width:=512; 
  291.   height:=1;
  292.   offset:=0;
  293.   SetImport('8-bit'); 
  294.   SetCustom(width,height,offset);
  295.   Import(''); {Read header}
  296.   GetPicSize(width,height);
  297.   if (width<>512) or (height<>1) then begin
  298.     Dispose;
  299.     PutMessage('Please to not change width, height, etc. in the Import dialog box.');
  300.     exit;
  301.   end;
  302.   width:=GetPixel(0,0)+GetPixel(1,0)*256;
  303.   height:=GetPixel(2,0)+GetPixel(3,0)*256;
  304.   nImages:=GetPixel(4,0)+GetPixel(5,0)*256;
  305.   Dispose;
  306.   hdrsize:= 76;
  307.   picsize:=width*height;
  308.   if (width<128) or (width>2048) or (height<128) or (height>2048) or (nImages<1) or (nImages>256) then begin
  309.     PutMessage('This does not seem to be a Biorad MRC 600 Z Series file.');
  310.     exit;
  311.   end;
  312.   start:=GetNumber('Starting image:',1);
  313.   offset:=HdrSize+(start-1)*PicSize;
  314.   SetImport('8-bit, Invert');
  315.   SetCustom(width,height,offset,nimages);
  316.   Import('');
  317.  ShowBioRadInfo;
  318. end;
  319.  
  320.  
  321. macro 'Import from IBAS';
  322. var
  323.    width,height,offset:integer;
  324. begin
  325.    width:=128; 
  326.    height:=1;
  327.    offset:=0;
  328.    SetImport('8-bit'); 
  329.    SetCustom(width,height,offset);
  330.    Import(''); {Read in header as an image, prompting for file name.}
  331.    width := (GetPixel(7,0)*256) + GetPixel(6,0);
  332.    height := (GetPixel(9,0)*256) + GetPixel(8,0);
  333.    Dispose(nPics);  {The ID of the last window opened = nPics.}
  334.    offset:=128;  {The IBAS offset}
  335.    SetImport('8-bit; Calibrate; Autoscale');
  336.    SetCustom(width,height,offset);
  337.    Import('');  {No prompt this time; Import remembers the name.}
  338.    Invert
  339.    SetScaling ('Bilinear');
  340.    SetScaling ('New Window');
  341.    ScaleAndRotate (0.80, 1.0, 0);
  342. end;
  343.  
  344.  
  345. macro 'Import 64x64x64x16-bit SPECT Image╔';
  346. {Imports a 64x64x64x16-bit headerless SPECT image into a stack.}
  347. var
  348.   width,height,nImages,hdrsize:integer;
  349. begin
  350.   RequiresVersion(1.50);
  351.   width:=64;
  352.   height:=64;
  353.   nImages:=64;
  354.   HdrSize:= 0;
  355.   SetImport('16-bit Unsigned, Swap Bytes');
  356.   {SetImportMinMax(0,2500);} {Uncomment to fix scale}
  357.   SetCustom(width,height,HdrSize,nImages);
  358.   Import('');
  359.  end;
  360.  
  361.  
  362. macro 'Import 8-bit 3D Image╔';
  363. var
  364.   width,height,offset,nImages:integer;
  365. begin
  366.   RequiresVersion(1.50);
  367.   width:=GetNumber('Width:',256);
  368.   height:=GetNumber('Height:',256);
  369.   nImages:=GetNumber('Depth(number of slices):',128);
  370.   offset:=GetNumber('Offset(header size):',0);
  371.   SetImport('8-bit'); 
  372.   SetCustom(width,height,offset,nImages);
  373.   Import('');
  374. end;
  375.  
  376.  
  377. macro 'Open with scale set to 100 pixels/mm [S]';
  378. {Example of a way to open images and have the}
  379. {spatial scale always set the same way.}  
  380. begin
  381.   Open('');           {or Import('')}
  382.   SetScale(100,'mm'); {Change as needed}
  383. end;
  384.  
  385.  
  386. macro 'Load Synergy Image';
  387. begin
  388.    SetImport('8-bit'); 
  389.    SetCustom(512,480,16384);
  390.    Import('');
  391.    ChangeValues(0,0,1);
  392.    ChangeValues(255,255,254);
  393.    SetPalette('Rainbow');
  394. end;
  395.  
  396.  
  397. macro 'Import Siemens 3D MRI╔';
  398. begin
  399.   RequiresVersion(1.50);
  400.   SetImport('16-bit Signed, Swap Bytes');
  401.   {SetImportMinMax(0,3000);} {Remove comments to fix scale}
  402.   SetCustom(256,256,0,127);
  403.   Import('');
  404. end;
  405.  
  406.  
  407. procedure ImportPhotoshop(offset:integer);
  408. {
  409. Imports a 24-bit, uncompressed Photoshop
  410. TIFF file into a 3-slice stack.
  411. }
  412. var
  413.   width,height,temp,stack,i,j:integer;
  414. begin
  415.   SetImport('8-bit; Invert');
  416.   width:=GetNumber('Width:',512);
  417.   height:=GetNumber('Height:',512);
  418.   SetCustom(3*width,height,offset,1);
  419.   Import('');
  420.   temp:=PicNumber;
  421.   SetNewSize(width,height);
  422.   MakeNewStack('RGB');
  423.   AddSlice;
  424.   AddSlice;
  425.   stack:=PicNumber;
  426.   for i:=0 to width-1 do begin
  427.     for j:=1 to 3 do begin
  428.       ChoosePic(temp);
  429.       GetColumn(3*i+(j-1),0,height);
  430.       ChoosePic(stack);
  431.       ChooseSlice(j);
  432.       PutColumn(i,0,height);
  433.     end;
  434.   end;
  435.   SelectSlice(1);
  436.   SelectPic(temp);
  437.   Dispose;
  438. end;
  439.  
  440. macro 'Import Photoshop 2.0 24-bit TIFF';
  441. begin
  442.   ImportPhotoshop(196);
  443. end;
  444.  
  445. macro 'Import Photoshop 2.5 24-bit TIFF';
  446. begin
  447.   ImportPhotoshop(598);
  448. end;
  449.  
  450. macro 'Import LUT╔';
  451. {Imports a 256 x 3 x 8-bit look-up table located 'offset'
  452.  bytes from the beginning of a file. Use an offset of 32 for
  453.  LUTs created using Image's Save As command and an
  454.  offset of 0 for Exported LUTs.}
  455. }  
  456. var
  457.   offset,i:integer;
  458. begin
  459.   offset:=0; {Use 32 for LUTs created using Save As}
  460.   SetImport('8-bit'); 
  461.   SetCustom(256,3,offset);
  462.   Import(''); {Read LUT as an image}
  463.   for i:=0 to 255 do begin
  464.     RedLut[i]:=GetPixel(i,0);
  465.     GreenLut[i]:=GetPixel(i,1);
  466.     BlueLut[i]:=GetPixel(i,2);
  467.   end;
  468.   UpdateLUT;
  469. end;
  470.  
  471. macro 'Open List of Files╔';
  472. var
  473.   width,height:integer;
  474.   name,ch:string;
  475. begin
  476.   width:=256;
  477.   height:=3;
  478.   SetCustom(width,height,0);
  479.   Import('File List'); {Read text as an image}
  480.   repeat
  481.     name:='';
  482.     repeat
  483.        ch:=chr(GetPixel(CharLoc mod width,CharLoc div width));
  484.        if ch>=chr(32) then begin
  485.          name:=concat(name,ch);
  486.        end;
  487.        CharLoc:=CharLoc+1;
  488.      until (ch=13) or (CharLoc>=width*height);
  489.      ShowMessage(name);
  490.      wait(1);
  491.   until (name='') or (CharLoc>=width*height);
  492. end;
  493.  
  494. macro 'File processing test';
  495. begin
  496.   Open('hd400:images:image001');
  497.   Invert;
  498.   Save;
  499.   Close;
  500.   Open('hd400:images:image002');
  501.   Invert;
  502.   Save;
  503.   Close;
  504.   Open('hd400:images:image003');
  505.   Invert;
  506.   Save;
  507.   Close;
  508. end;
  509.  
  510. macro 'Batch Processing Example╔';
  511. {
  512. Reads from disk and processes a set of images too large to
  513. simultaneously fit in memory. The image names names must be
  514. in the form 'image001', 'image002', ..., but this can be changed.
  515. }
  516. var
  517.   i:integer;
  518. begin
  519.   for i:=1 to 1000 do begin
  520.       open('image',i:3);
  521.      {process;}
  522.       save;
  523.       close;
  524.    end;
  525. end;
  526.  
  527.  
  528. macro 'Export Non-Rectangular ROI';
  529. var
  530.   left,top,width,height:integer;
  531.   pid1,pid2:integer;
  532. begin
  533.   GetRoi(left,top,width,height);
  534.   if width=0 then begin
  535.      PutMessage('Non-rectangular selection required.');
  536.      exit;
  537.   end;
  538.   pid1:=PidNumber;
  539.   SetNewSize(width,height);
  540.   MakeNewWindow('Temp');
  541.   pid2:=PidNumber;
  542.   SelectPic(pid1);
  543.   Copy;
  544.   SelectPic(pid2);
  545.   Paste;
  546.   Export; {or SaveAs}
  547. end;
  548.  
  549.  
  550. macro 'Import MCID╔';
  551. begin
  552.   SetImport('MCID'); 
  553.   Import(''); 
  554. end;
  555.  
  556.  
  557. macro 'Import Series of Files╔';
  558. Var
  559.   i,first,last:integer;
  560.   width, height, offset:integer;
  561. begin
  562.   width := 512;
  563.   height := 512;
  564.   offset := 0;
  565.   first:=round(GetNumber('First file number:',1));
  566.   last:=round(GetNumber('Last file number:',999));
  567.   SetImport('Custom; 8-bits; Invert');
  568.   SetCustom(width,height,offset);
  569.   for i:=first to last do
  570.     Import('IMG',i:4, '.CASTRO11');
  571. end;
  572.  
  573.  
  574.